local dx_net = require("dx_net")
local logger = require("dx_logger")
local config = require("config")
local dx_gpio = require("dx_gpio")
local time = require("time")

-------------------变量区-------------------
local network_service = {}

-- 入网方式（网口、wifi、4g）
NET_TYPE_NONE =  0x00
NET_TYPE_ETH  =  0x01
NET_TYPE_WIFI =  0x02
NET_TYPE_MODEM   =  0x04


-- 入网模式（静态、动态）
NET_MODE_STATIC     = "1"
NET_MODE_DHCP       = "2"
NET_MODE_WIFI_AP    = "3"


-- 网络状态（）
-- 未初始态
NET_STATUS_NONE = 0
-- 网卡处于关闭状态
NET_STATUS_DOWN = 1
-- 网卡处于打开状态
NET_STATUS_UP = 2
-- 网线已插入或者wifi已连接ssid 但未分配ip
NET_STATUS_CONNECTING_ROUTE = 3
-- 已成功分配ip
NET_STATUS_CONNECTED_ROUTE = 4
-- 已连接指定服务或者通过测试可以连接到广域网
NET_STATUS_CONNECTED_SERVER = 5


-------------------函数区-------------------
function network_service.config_net()
    logger.info("[config_net]网络配置初始化")
    dx_net.net_init()
    local net_net_type = config.get("net_net_type")
    local net_net_mode = config.get("net_net_mode")
    dx_net.vbar_net_card_enable(tonumber(net_net_type, 16), true)

    local net_static_param = nil
    if net_net_mode == NET_MODE_STATIC then
        net_static_param = {ip = config.get("net_local_ip"), gateway = config.get("net_local_gateway"),
        netmask = config.get("net_local_netmask"), dns0 = config.get("net_local_dns"), dns1 = config.get("net_local_dns")}
    end

    dx_net.vbar_net_set_mode_by_card(tonumber(net_net_type, 16), tonumber(net_net_mode, 10), net_static_param)
    -- 注册网络状态回调
    dx_net.net_status_callback_register(network_service.func_cb)
end



-- 获取最新网络参数配置
function network_service.get_net_info()
    local net_param = dx_net.vbar_net_get_mode_by_card(tonumber(config.get("net_net_type"), 16))
    -- 获取网络信息
    config.set("net_local_ip", network_service.toAscii(net_param.ip, 20))
    config.set("net_local_gateway", network_service.toAscii(net_param.gateway, 20))
    config.set("net_local_dns", network_service.toAscii(net_param.dns0, 20))
    config.set("net_local_netmask", network_service.toAscii(net_param.netmask, 20))
end


-- 网络状态回调函数
function network_service.func_cb(type, status, pdata)
    if status == NET_STATUS_NONE then
        -- 未初始态
        logger.info("[net_status] State changes to NET_STATUS_NONE "..NET_STATUS_NONE)
    elseif status == NET_STATUS_DOWN then
        -- 网卡处于打开状态
        logger.info("[net_status] State changes to NET_STATUS_DOWN "..NET_STATUS_DOWN)
    elseif status == NET_STATUS_UP then
        -- 网卡处于打开状态
        logger.info("[net_status] State changes to NET_STATUS_UP "..NET_STATUS_UP)
    elseif status == NET_STATUS_CONNECTING_ROUTE then
        -- 网线已插入或者wifi已连接ssid 但未分配ip
        logger.info("[net_status] State changes to NET_STATUS_CONNECTING_ROUTE "..NET_STATUS_CONNECTING_ROUTE)
    elseif status == NET_STATUS_CONNECTED_ROUTE then
        -- 已成功分配ip
        logger.info("[net_status] State changes to NET_STATUS_CONNECTED_ROUTE "..NET_STATUS_CONNECTED_ROUTE)
        config.set("network_status", "true")
        time.calibration_time()
        if config.get("mqtt_con_status") == "true" then
            dx_gpio.gpio_set_value(73, 1)
        end
    elseif status == NET_STATUS_CONNECTED_SERVER then
        -- 已连接指定服务或者通过测试可以连接到广域网
        logger.info("[net_status] State changes to NET_STATUS_CONNECTED_SERVER "..NET_STATUS_CONNECTED_SERVER)
    end
    if status ~= NET_STATUS_CONNECTED_ROUTE then
        dx_gpio.gpio_set_value(73, 0)
    end

    -- 获取最新网络参数配置
    network_service.get_net_info()
end


function network_service.toAscii(strings, len)
    local i = 0
    local str = ""
    while i < len do
        -- 跳过空白字符
        if string.byte(string.char(strings[i])) ~= 0 then
            str = str .. string.char(strings[i])
        end
        i = i + 1
    end
    return str
end

return network_service
